package com.resou.reader.signin.style1;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.ReSouApplication;
import com.resou.reader.data.signin.model.SignInInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 显示签到任务列表的适配器
 */
public class SignInRuleAdapter extends RecyclerView.Adapter {
    private static final String TAG = "SignInAdapter";
    private static final int sign_task = 0;
    private static final int sign_in_rule = 1;
    private List<Object> mList;

    public SignInRuleAdapter(List<SignInInfo.UserSignTaskBean> userSignTask, List<SignInInfo.SignInRuleBean> signInRule) {
        mList = new ArrayList<>();
        mList.addAll(userSignTask);
        mList.add("签到规则");
        mList.addAll(signInRule);
    }

    private OnTaskInteractionListener mInteractionListener;

    public void setInteractionListener(OnTaskInteractionListener interactionListener) {
        mInteractionListener = interactionListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        if (viewType == sign_task) {
            //任务列表
            return new SinInTaskHolder(layoutInflater.inflate(R.layout.recycler_item_sign_in_task_item, parent, false));
        }
        if (viewType == sign_in_rule) {
            //任务规则
            View view = layoutInflater.inflate(R.layout.recycler_item_sign_in_rule, parent, false);
            return new SinInRuleHolder(view);
        }
        //默认文本
        View view = layoutInflater.inflate(R.layout.recycler_item_sign_in_rule, parent, false);
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.topMargin = 20;
        layoutParams.bottomMargin = 20;
        return new DefaultHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof SinInTaskHolder) {
            SignInInfo.UserSignTaskBean taskBean = (SignInInfo.UserSignTaskBean) mList.get(position);
            SinInTaskHolder holder = (SinInTaskHolder) viewHolder;
            holder.tvRuleInfo.setText(taskBean.getTaskDesc());
            if (Integer.valueOf(taskBean.getTaskStatus()) == 0) {
                holder.tvSignStatus.setText("未完成");
                holder.tvSignStatus.setTextColor(Color.RED);
                holder.tvSignStatus.setBackgroundResource(R.drawable.bg_sign_in_red_hollow_button);
                mInteractionListener.onToCompleteClicked();
            }
            if (Integer.valueOf(taskBean.getTaskStatus()) == 1) {
                holder.tvSignStatus.setText("领取");
                holder.tvSignStatus.setTextColor(Color.WHITE);
                holder.tvSignStatus.setBackgroundResource(R.drawable.bg_sign_in_red_solid_button);
                holder.tvSignStatus.setOnClickListener(v -> {
                    mInteractionListener.onReceiveClicked(taskBean.getTaskId());
                });
            }
            if (Integer.valueOf(taskBean.getTaskStatus()) == 2) {
                holder.tvSignStatus.setText("已完成");
                holder.tvSignStatus.setTextColor(ReSouApplication.getRSApplication().getResources().getColor(R.color.CCCCCC));
                holder.tvSignStatus.setBackgroundResource(R.drawable.bg_sign_in_light_hollow_button);
            }
        }
        if (viewHolder instanceof SinInRuleHolder) {
            SignInInfo.SignInRuleBean ruleBean = (SignInInfo.SignInRuleBean) mList.get(position);
            SinInRuleHolder holder = (SinInRuleHolder) viewHolder;
            holder.tvSignInRule.setText(ruleBean.getRuleDesc());
        }
        if (viewHolder instanceof DefaultHolder) {
            DefaultHolder holder = (DefaultHolder) viewHolder;
            holder.tvSignInRule.setText((String) mList.get(position));
            holder.tvSignInRule.setTextColor(ReSouApplication.getRSApplication().getResources().getColor(R.color.CCCCCC));
            holder.tvSignInRule.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        Object o = mList.get(position);
        if (o instanceof SignInInfo.UserSignTaskBean) {
            return sign_task;
        }
        if (o instanceof SignInInfo.SignInRuleBean) {
            return sign_in_rule;
        }
        return -1;
    }

    static class SinInTaskHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_rule_info)
        TextView tvRuleInfo;
        @BindView(R.id.tv_sign_status)
        TextView tvSignStatus;

        public SinInTaskHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class SinInRuleHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_sign_in_rule)
        TextView tvSignInRule;

        public SinInRuleHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_sign_in_rule)
        TextView tvSignInRule;

        public DefaultHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnTaskInteractionListener {
        void onToCompleteClicked();

        void onReceiveClicked(String taskId);
    }

}

